package com.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cs.constant.ProdCommConstant;
import com.cs.controller.ProdCommController;
import com.cs.domain.Prod;
import com.cs.domain.ProdComm;
import com.cs.mapper.ProdCommMapper;
import com.cs.mapper.ProdMapper;
import com.cs.service.ProdCommService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@CacheConfig(cacheNames = "com.cs.service.impl.ProdCommServiceImpl")
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm> implements ProdCommService {

    @Autowired
    private ProdCommMapper prodCommMapper;
    @Autowired
    private ProdMapper prodMapper;

    /**
     * 分页查询评论
     *
     * @param page
     * @param prodComm
     * @return
     */
    @Override
    @Cacheable(key = ProdCommConstant.PRODCOMM_PAGE_PREFIX)
    public Page<ProdComm> findProdCommPage(Page<ProdComm> page, ProdComm prodComm) {
        Page<ProdComm> prodCommPage = null;
        List<Long> proIds = null;
        List<Prod> prods = null;
        //商品的名称
        String prodName = prodComm.getProdName();
        if (StringUtils.hasText(prodName)) {
            //因为设计的评论表中没有商品名称字段，所以要查询出来
            prods = prodMapper.selectList(new LambdaQueryWrapper<Prod>().eq(Prod::getProdName, prodName));

            if (!CollectionUtils.isEmpty(prods)) {
                proIds = prods.stream().map(Prod::getProdId).collect(Collectors.toList());
            } else {//没有到商品没有评论
                prodCommPage = new Page<ProdComm>(page.getCurrent(), page.getSize());
                prodCommPage.setRecords(Collections.emptyList());
                prodCommPage.setTotal(0L);
            }
        }

        //商品的id不为空的情况
        page.addOrder(OrderItem.desc("rec_time"));
        //评论的分页对象
        prodCommPage = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>().eq(!ObjectUtils.isEmpty(prodComm.getStatus()), ProdComm::getStatus, prodComm.getStatus())
                .in(!CollectionUtils.isEmpty(proIds), ProdComm::getProdId, prods));
        //组装返回的数据
        List<ProdComm> commPageRecords = prodCommPage.getRecords();
        if (!CollectionUtils.isEmpty(commPageRecords)) {
            //商品的id集合
            List<Long> prodIds = commPageRecords.stream().map(ProdComm::getProdId).collect(Collectors.toList());

            List<Prod> prods1 = prodMapper.selectBatchIds(prodIds);

            commPageRecords.forEach(prodComm1 -> {
                Prod prod1 = prods1.stream().filter(prod -> prod.getProdId().equals(prodComm1.getProdId())).collect(Collectors.toList()).get(0);
                prodComm1.setProdName(prod1.getProdName());
            });
        }

        return prodCommPage;
    }
}
